package br.com.virtualclassroom;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class SystemFunctions {

	// Public Strings Declaration
	private static String path;

	// Private Strings Declaration
	public static String userID, password, url, inputValue;

	// Create the method for Print Logs
	public static void printLog(boolean error, String messageError) {

		String direction = null;

		if (error == true)
			direction = " <- ";

		else if (error == false)
			direction = " -> ";

		if (path == null) {
			try {
				getProperties();
			} catch (Exception e) {
				System.out.println("Error: " + e.toString());
				System.exit(0);
			}
		}

		String date = ((new SimpleDateFormat("yyyyMMdd")).format(new Date()))
				.toString();

		path = path + "log" + date + ".txt";

		PrintWriter pwLog = null;

		try {
			pwLog = new PrintWriter(new FileOutputStream(path, true), true);
		} catch (FileNotFoundException e) {
			System.out.println("Error " + e.toString());
		}

		String tmpDate = ((new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"))
				.format(new Date())).toString();

		pwLog.println(tmpDate + direction + messageError);
	}

	// Get the parameters of file "VirtualClassroom.properties"
	public static void getProperties() {

		Properties props = new Properties();
		InputStream inputStream = null;

		try {
			inputStream = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("VirtualClassroom.properties");
			props.load(inputStream);

			if (inputStream == null) {
				System.err.println("Error\nPlease check the properties file");
			}

			inputStream.close();
		} catch (IOException ex) {
			System.err.println("IOException " + ex.getMessage());
			ex.printStackTrace();
		}

		userID = props.getProperty("parameter.user");
		password = props.getProperty("parameter.password");
		url = props.getProperty("parameter.database");
		path = props.getProperty("parameter.logfile");
	}

	// Get the parameters of file "VirtualClassroom.properties"
	public static void setProperties(String tmpUser, String tmpPass,
			String tmpLog) {

		Properties props = new Properties();

		if (tmpLog.length() < 1) {
			tmpLog = "/Users/Raul/Desktop/";
		}

		props.setProperty("parameter.user", tmpUser);
		props.setProperty("parameter.password", tmpPass);
		props.setProperty("parameter.database", url);
		props.setProperty("parameter.logfile", tmpLog);

		try {
			props.store(new FileOutputStream("VirtualClassroom.properties"),
					"Virtual Properties File Configuration");
		} catch (FileNotFoundException e) {
			printLog(true, "Error: " + e.getMessage());
		} catch (IOException e) {
			printLog(true, "Error: " + e.toString());
		}
	}

	public static String defineUrl(String urlTemp) {

		url = "jdbc:mysql://localhost:3306/" + urlTemp;
		
		return url;
	}
}
